'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface, Sequelize) {
    try {
      // 删除旧的唯一约束
      await queryInterface.sequelize.query(
        'ALTER TABLE `friendships` DROP INDEX `friendships_user_id_friend_id`'
      );
      
      // 添加新的包含 status 的唯一索引
      await queryInterface.addIndex('friendships', ['userId', 'friendId', 'status'], {
        unique: true,
        name: 'unique_friendship_with_status'
      });
    } catch (error) {
      console.error('Migration failed:', error);
      throw error;
    }
  },

  async down(queryInterface, Sequelize) {
    try {
      // 删除新添加的索引
      await queryInterface.sequelize.query(
        'ALTER TABLE `friendships` DROP INDEX `unique_friendship_with_status`'
      );
      
      // 恢复原来的唯一约束
      await queryInterface.addIndex('friendships', ['userId', 'friendId'], {
        unique: true,
        name: 'friendships_user_id_friend_id'
      });
    } catch (error) {
      console.error('Migration rollback failed:', error);
      throw error;
    }
  }
};
